package com.sensePlusVR.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sensePlusVR.adapter.FollowAllAdapter;
import com.sensePlusVR.base.BaseFragment;
import com.sensePlusVR.bean.ActorItem;
import com.sensePlusVR.main.R;
import com.sensePlusVR.manager.ActorManager;
import com.sensePlusVR.manager.DataCenter;
import com.sensePlusVR.manager.EventListener;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.widget.FullyGridLayoutManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.fragment_follow_all)
public class FollowAllFragment extends BaseFragment{
	private static final String TAG = FollowAllFragment.class.getSimpleName();
	private final String KEY=this.getClass().getSimpleName();
	private DataCenter dataCenter;
	private List<ActorItem> followAllList;
	private FollowAllAdapter followAllAdapter;
	
	@ViewInject(R.id.follow_all_recycler)
	private RecyclerView follow_all_recycler;

	@SuppressWarnings("unchecked")
	EventListener listenerActors = new EventListener(){
		@Override
		public void Notify(int event, Object data, Object Params) {
			if (Params == null || data == null)
				return;
			MyLog.d(TAG, "listenerActors event : " + event);
			if (event == ActorManager.EventActorAllGot) {
				List<ActorItem> list = (List<ActorItem>) data;
				if (list != null && list.size() > 0) {
					followAllList.clear();
					followAllList.addAll(list);
					followAllAdapter.refreshData();
				}
			}
		}
	};
	
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dataCenter = DataCenter.getInstans();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = x.view().inject(this, inflater, container);
		beforeView();
		initView();
		dataCenter.addActorEventListener(listenerActors, this, KEY);
		return rootView;
	}
	
	private void beforeView() {
		//int userId = MySharedPreferences.getInteger2(getActivity(),MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_ID);
		if (followAllList == null) {
			followAllList = new ArrayList<ActorItem>();
		}
		followAllList.clear();
		
		if (followAllList.size() == 0){
			List<ActorItem> list = dataCenter.getActors(false);
			if (list != null)
				followAllList.addAll(list);
		}
		followAllAdapter = new FollowAllAdapter(getActivity(),followAllList);
	}
	
	private void initView() {
		FullyGridLayoutManager gridLayoutManager = new FullyGridLayoutManager(getActivity(), 2);
		gridLayoutManager.setSmoothScrollbarEnabled(true);
		gridLayoutManager.setAutoMeasureEnabled(true);
		follow_all_recycler.setLayoutManager(gridLayoutManager);
		follow_all_recycler.setAdapter(followAllAdapter);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		if (followAllAdapter != null) {
			followAllAdapter.onPause();
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if (followAllAdapter != null) {
			followAllAdapter.onResume();
		}
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		dataCenter.delActorEventListener(KEY);
	}

}
